var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        ladderPriceDialogVisible:false,
		//定义单位:
        unit:'张',
		quoteFastPrintGroupPrice: {},
		//接受左侧table相关数据
        fastPrintMaterialData:[],
		//右侧价格配置数据:
        fastPrintGroupPriceData:[],
		//设置阶梯价：
        ladderPriceData:[],
        //当前阶梯价所在option 行
        ladderPriceRow: null,
        //记录左侧table查看详情时所行
        fastPrintMaterialSelectRow:null,
		//保存配价时选中行
        groupPriceRows:[],
		//颜色列表
        colourData:[{ value:'blackwhite',label: '黑白'},{value:'multicolour',label:'彩色'}],
        //分页相关参数
        total: 0,
        pagesize:10,
        currentPage:1,
        //右侧table进行克隆
        oldData:[],
        //右侧table搜索条件
        q:{optionName2:'',paperLength:'',paperWide:''}
	},
    mounted: function () {
        this.initData();
    },
	methods: {
        /*页面初始化数据*/
        initData : function () {
        	//查到工厂下已存在的颜色，材料
            $.get(baseURL + "quoteFastPrintGroupPrice/quotefastprintgroupprice/fastPrintMaterialList", function(r){
                vm.fastPrintMaterialData = r.fastPrintMaterialData;
            });
		},
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.quoteFastPrintGroupPrice = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.quoteFastPrintGroupPrice.id == null ? "quoteFastPrintGroupPrice/quotefastprintgroupprice/save" : "quoteFastPrintGroupPrice/quotefastprintgroupprice/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.quoteFastPrintGroupPrice),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
            var postData =vm.groupPriceRows;
            if(postData.length == 0){
                this.$message.error('至少选中一个选项 !');
                return;
            }
            var self = this;
			var ids =[];
			for (var i=0;i<postData.length;i++){
                ids.push(postData[i].id);
            }
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "quoteFastPrintGroupPrice/quotefastprintgroupprice/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
                                vm.refreshGroupPriceTable();
                                vm.initData();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "quoteFastPrintGroupPrice/quotefastprintgroupprice/info/"+id, function(r){
                vm.quoteFastPrintGroupPrice = r.quoteFastPrintGroupPrice;
            });
		},
        queryLeftTable:function(){
            var url = "quoteFastPrintGroupPrice/quotefastprintgroupprice/queryfastPrintMaterialList";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data:JSON.stringify(vm.quoteFastPrintGroupPrice),
                success: function(r){
                    if(r.code === 0){
                    	//清空查询数据
                        vm.quoteFastPrintGroupPrice={},
                        vm.fastPrintMaterialData = r.fastPrintMaterialData;
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},
		//处理颜色显示问题
        colourFormat(row, column){
			if(row.colour=="multicolour"){
                return '彩色';
			}
			if(row.colour=="blackwhite"){
                return '黑白';
			}
		},
		//查看详情
        detail:function (index, rows){
            var row = rows[index];
            vm.fastPrintMaterialSelectRow=row;
        	vm.quoteFastPrintGroupPrice.colour=row.colour;
            vm.quoteFastPrintGroupPrice.optionName=row.optionName;
            $.ajax({
                type: "POST",
                url: baseURL + "quoteFastPrintGroupPrice/quotefastprintgroupprice/list",
                contentType: "application/json",
                data:JSON.stringify(vm.quoteFastPrintGroupPrice),
                success: function(r){
                    if(r.code === 0){
                      vm.fastPrintGroupPriceData=r.fastPrintGroupPriceData;
                      vm.total=vm.fastPrintGroupPriceData.length;
                        //备份数据
                      vm.oldData=JSON.parse(JSON.stringify(vm.fastPrintGroupPriceData));
                    }else{
                        alert(r.msg);
                    }
                }
            });
		},

        /*设置阶梯价格*/
        openLadderPriceDialog: function (row) {
            //先初始化数据
            if(row.ladderPrice){
                vm.ladderPriceData =JSON.parse(row.ladderPrice);
            }else {
                vm.ladderPriceData = [];
                var temp={"startNum": null, "endNum": null, "unitPrice": null , "remark": null};
                // 加入一条空白数据
                vm.ladderPriceData.push(temp);
            }
            // 保留当前option 所在行
            vm.ladderPriceRow = row;
            vm.ladderPriceDialogVisible = true;
        },/*保存阶梯价*/
        saveStepPrice: function () {
            //todo 数据校验
            var row = vm.ladderPriceRow;
            row.ladderPrice =JSON.stringify(vm.ladderPriceData) ;
            // 清空临时数据
            vm.ladderPriceRow = null;
            vm.ladderPriceData = [];
            vm.ladderPriceDialogVisible = false;
        },
        /*添加一行阶梯价*/
        addOneLadderPrice: function () {
            var temp={"startNum": null, "endNum": null, "unitPrice": null , "remark": null};
            vm.ladderPriceData.push(temp);
        },

        /*删除一行数据*/
        deleteOneLadderPrice: function (index, rows) {
            if(rows.length >1){
                rows.splice(index, 1);
            }
        },
        /*批量保存配价*/
        saveGroupPrice: function () {
            var postData =vm.groupPriceRows;
            if(postData.length == 0){
                this.$message.error('至少选中一个选项 !');
                return;
            }
            var self = this;

            $.ajax({
                type: "POST",
                url: baseURL + "quoteFastPrintGroupPrice/quotefastprintgroupprice/save",
                contentType: "application/json",
                data:JSON.stringify(postData),
                success: function(r){
                    if(r.code === 0){
                        self.$message({message: '保存成功 !' ,type: 'success'});
                        vm.initData();
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        /*获取快印组合配价勾选项*/
        groupPriceSelectionChange(val){
        	vm.groupPriceRows=val;
		},
        /*刷新右侧table*/
        refreshGroupPriceTable:function () {
            $.ajax({
                type: "POST",
                url: baseURL + "quoteFastPrintGroupPrice/quotefastprintgroupprice/list",
                contentType: "application/json",
                data:JSON.stringify(vm.quoteFastPrintGroupPrice),
                success: function(r){
                    if(r.code === 0){
                        vm.fastPrintGroupPriceData=r.fastPrintGroupPriceData;
                        vm.total=vm.fastPrintGroupPriceData.length;
                        vm.oldData=JSON.parse(JSON.stringify(vm.fastPrintGroupPriceData));
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        /*处理分页*/
        handleSizeChange(val) {
            this.pagesize = val;
        },
        handleCurrentChange(val) {
            this.currentPage = val;
        },
        //筛选右侧table数据
        queryGroupPriceTable:function(){
            vm.fastPrintGroupPriceData=vm.oldData;
            //克重筛选
            if(vm.q.optionName2){
                vm.fastPrintGroupPriceData=vm.fastPrintGroupPriceData.filter(item=>(item.optionName2==vm.q.optionName2));
            }
            //尺寸长筛选
            if(vm.q.paperLength){
                vm.fastPrintGroupPriceData=vm.fastPrintGroupPriceData.filter(item=>(item.paperLength==vm.q.paperLength));
            }
            //尺寸宽筛选
            if(vm.q.paperWide){
                vm.fastPrintGroupPriceData=vm.fastPrintGroupPriceData.filter(item=>(item.paperWide==vm.q.paperWide));
            }
            vm.total=vm.fastPrintGroupPriceData.length;
        }
    }
});